package com.bbs.crawl;

import java.util.List;
import java.util.regex.Matcher;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bbs.bean.BBSOfXcar;
import com.car.util.DocUtil;
import com.car.util.Util;

public class BBSCrawlOfXcar {
	public int getPages(int bbsid) {
		int page = 1;
		String url = "http://www.xcar.com.cn/bbs/forumdisplay.php?fid=" + bbsid;
		Document doc = DocUtil.getDoc(url);
		if (doc == null) {
			System.out.println("PAGE0" + "\t" + url);
			return 0;
		}
		try {
			Elements pageNum = doc.select("div.page2013 a");
			String pageString = pageNum.get(pageNum.size() - 3).text();
			page = Integer.parseInt(pageString);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(url);
		}
		return page;
	}

	public void startCrawl(int bbsid) {
		int page = getPages(bbsid);
		for (int i = 1; i < 2; i++) {
			String url = "http://www.xcar.com.cn/bbs/forumdisplay.php?fid=" + bbsid + "&page=" + i;
			Document doc = DocUtil.getDoc(url);
			if (doc == null) {
				System.out.println("PP0" + "\t" + url);
				return;
			}
			Elements row = doc.select("table.row td.line34 a.open_view");
			for (Element a : row) {
				String link = a.attr("href");
				if (!link.contains("xcar")) {
					link = "http://www.xcar.com.cn" + link;
					parseContent(link, bbsid);
				}
			}
		}
	}

	public void parseContent(String url, int bbsid) {
		Document doc = DocUtil.getDoc(url);
		if (doc.html().contains("抱歉，您无法进行当前操作，可能的原因是：")) {
			System.out.println(url + "\t" + "无访问权");
			return;
		}
		try {
			if (doc == null) {
				System.out.println("TieZi0" + "\t" + url);
				return;
			}
			int bbs_id = Integer.parseInt(Util.getMatched(url, "(.*=)(\\d{1,})").group(2));
			int bbs_ppid = bbsid;
			String bbs_bt = doc.select("span.t_title1").text();
			int bbs_sfjh = doc.select("td.bold a").text().contains("加入精华") ? 1 : 0;
			int bbs_sfwd = doc.select("span.question_s_title").size() > 0 ? 1 : 0;
			String bbs_nr = bbs_sfwd == 0 ? doc.select("table.t_msg tr div.t_msgfont1").text()
					: doc.select("div.question_info").text();
			String bbs_fbsj = doc.select("table.t_msg tr").get(0).select("div:not(.right)").text().split("发表于 ")[1]
					.split(" ")[0];
			Element dd = doc.select("p#showPic span").get(0);
			int bbs_djs = Integer.parseInt(Util.getMatched(dd.text().split("\\s+")[1], "\\d+").group(0));
			int bbs_hfs = Integer.parseInt(Util.getMatched(dd.text().split("  ")[0], "\\d+").group(0));
			Element userInfo = doc.select("td.t_user").get(0);
			int bbs_yhid = Integer.parseInt(userInfo.select("a.bold").get(0).attr("href").toString().split("uid=")[1]);
			String bbs_yhmc = userInfo.select("a.bold").get(0).text();
			String ss = doc.select("div.smalltxt").get(0).select("p").text();
			Matcher m = Util.getMatched(ss,
					"(.*)(财产:\\s)(\\d+)(\\s+爱卡币.*帖子:\\s+)(\\d+)(.*注册:\\s)(\\d{4}-\\d{2}-\\d{2})(.*)");
			String bbs_dj = "";
			int bbs_cc = 0;
			int bbs_tz = 0;
			String bbs_zc = "";
			if (m != null) {
				bbs_dj = m.group(1).trim();
				bbs_cc = Integer.parseInt(m.group(3));
				bbs_tz = Integer.parseInt(m.group(5));
				bbs_zc = m.group(7);
			}
			BBSOfXcar bbs = new BBSOfXcar(bbs_id, bbs_ppid, bbs_bt, bbs_sfjh, bbs_sfwd, bbs_nr, bbs_fbsj, bbs_djs,
					bbs_hfs, bbs_yhid, bbs_yhmc, bbs_dj, bbs_cc, bbs_tz, bbs_zc);
		} catch (Exception e) {
			System.out.println(url);
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		BBSCrawlOfXcar bbs = new BBSCrawlOfXcar();
		List<Integer> ids = new ReployOfXcar().getBBSId();
		for (Integer id : ids) {
			bbs.startCrawl(id);
		}
	}
}
